package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SourceSubcategoryMapper;
import com.ruoyi.system.domain.SourceSubcategory;
import com.ruoyi.system.service.ISourceSubcategoryService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 来源小类Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-09
 */
@Service
public class SourceSubcategoryServiceImpl implements ISourceSubcategoryService
{
    @Autowired
    private SourceSubcategoryMapper sourceSubcategoryMapper;

    /**
     * 查询来源小类
     *
     * @param id 来源小类主键
     * @return 来源小类
     */
    @Override
    public SourceSubcategory selectSourceSubcategoryById(Long id)
    {
        return sourceSubcategoryMapper.selectSourceSubcategoryById(id);
    }

    /**
     * 根据大类主键与名称查询来源小类
     * @param name
     * @param leadId
     * @return
     */
    @Override
    public SourceSubcategory selectSourceSubcategoryByNameAndLeadId(String name, Long leadId) {
        return sourceSubcategoryMapper.selectSourceSubcategoryByNameAndLeadId(name,leadId);
    }

    /**
     * 查询来源小类列表
     *
     * @param sourceSubcategory 来源小类
     * @return 来源小类
     */
    @Override
    public List<SourceSubcategory> selectSourceSubcategoryList(SourceSubcategory sourceSubcategory)
    {
        if (null == sourceSubcategory.getSourceOfLeadCustomersId()){
            List<SourceSubcategory> sourceSubcategoryList = new ArrayList<>();
            return sourceSubcategoryList;
        }
        return sourceSubcategoryMapper.selectSourceSubcategoryList(sourceSubcategory);
    }

    /**
     * 新增来源小类
     *
     * @param sourceSubcategory 来源小类
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSourceSubcategory(SourceSubcategory sourceSubcategory)
    {
        int status = 0;
        try {
            //新增来源小类判空方法
            emptyInsertSourceSubcategory(sourceSubcategory);
            //新增来源小类方法
            status = sourceSubcategoryMapper.insertSourceSubcategory(sourceSubcategory);
        }
        catch (Exception e){
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException(e.getMessage());
        }
        return status;
    }

    /**
     * 新增来源小类判空方法
     * @param sourceSubcategory
     */
    @Override
    public void emptyInsertSourceSubcategory(SourceSubcategory sourceSubcategory) {
        //来源小类名称
        if (StringUtils.isEmpty(sourceSubcategory.getName())) {
            throw new RuntimeException("来源小磊名称未填写");
        }
    }

    /**
     * 修改来源小类
     *
     * @param sourceSubcategory 来源小类
     * @return 结果
     */
    @Override
    public int updateSourceSubcategory(SourceSubcategory sourceSubcategory)
    {
        return sourceSubcategoryMapper.updateSourceSubcategory(sourceSubcategory);
    }

    /**
     * 批量删除来源小类
     *
     * @param ids 需要删除的来源小类主键
     * @return 结果
     */
    @Override
    public int deleteSourceSubcategoryByIds(Long[] ids)
    {
        return sourceSubcategoryMapper.deleteSourceSubcategoryByIds(ids);
    }

    /**
     * 删除来源小类信息
     *
     * @param id 来源小类主键
     * @return 结果
     */
    @Override
    public int deleteSourceSubcategoryById(Long id)
    {
        return sourceSubcategoryMapper.deleteSourceSubcategoryById(id);
    }
}